package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionPlan;
import com.sduept.bigdata.equipment.assessment.service.InspectionPlanService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.Calendar;
import java.util.List;

/**
 * 执行评价计划定时任务
 *
 * @author pengz
 */

@Service
public class AssessmentPlanExecuteTimerSchedule {
    @Inject
    private InspectionPlanService planService;

    //	@Schedule(hour = "0", minute = "0", second = "0")
    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        List<InspectionPlan> unexecutedPlan = planService.findInspectionPlanByCondition(null, null, null, null, 1);
        for (InspectionPlan inspectionPlan : unexecutedPlan) {
            if (inspectionPlan.getStartTime().after(start.getTime()) && inspectionPlan.getStartTime().before(end.getTime())) {
                inspectionPlan.setStatus(2);
                inspectionPlan.setActualStartTime(inspectionPlan.getStartTime());
                planService.createOrupdate(inspectionPlan);
            }
        }
    }

}
